package cn.mogugo.comm.quartz;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.mogugo.comm.BaseAction;
import cn.mogugo.comm.CC;
import cn.mogugo.joke.model.JokeObj;
import cn.mogugo.util.tools.DateTools;

/**
 * 保证每日至少将100篇笑话置顶
 * @author Administrator
 *
 */
public class JokeObjQuartz extends BaseAction {
	
	public void updateJokeForEveryDay(){
		
		try{
			//状态为99的数据为后期手工录入的
			
			Date curTime = DateTools.getCurDateTime();
			Calendar c = Calendar.getInstance();
			c.setTime(curTime);
			
			List list99 = this.jokeObjManager.getJokeObjByAuditing(99);
			int totalSize = 100;
			int size99 = 0;
			int sizeRandom = totalSize - size99;
			if(!list99.isEmpty()){
				if(list99.size() >= totalSize){
					size99 = totalSize;
					sizeRandom = 0;
				}else{
					size99 = list99.size();
					sizeRandom = totalSize - size99;
				}
			}
			
			if(size99 > 0){
				for(int i = 0 ; i < size99 ; i ++ ){
					JokeObj jo = (JokeObj)list99.get(i);
					jo.setAuditing(1);
					c.add(Calendar.MINUTE, 1);
					jo.setJoke_date(c.getTime());
					this.baseDao.saveOrUpdate(jo);
				}
			}

			
			
			if(sizeRandom > 0){
				List listRandom = this.jokeObjManager.getListRandomByJokeDate(CC.updateDateString, sizeRandom);
				System.out.println("listRandom.size = " + listRandom.size());
				for(int i = 0 ; i < listRandom.size() ; i ++ ){
					Map map = (Map)listRandom.get(i);
					Integer jokeObjId = (Integer)map.get("ID");
					JokeObj jo = (JokeObj)this.baseDao.get(JokeObj.class, jokeObjId);
					jo.setAuditing(1);
					c.add(Calendar.MINUTE, 1);
					jo.setJoke_date(c.getTime());
					this.baseDao.saveOrUpdate(jo);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		

		
	}
}
